package struts.action;

import hibernate.PageInfo;
import hibernate.pojo.Users;

import java.util.List;

import common.MD5Sec;

public class UserMan extends BaseAction {

	// public List list;
	public PageInfo pi;
	public String pageNo;
	public String pageCount;
	public String condi; // 查询条件
	public String command;

	// 删除使用的主键结果集
	public String ids;

	// 返回结果
	public boolean result;
	public String rtntxt;

	// 增加使用的forms内容
	public String id;
	public String userid;
	public String username;
	public String userpass;
	public String memo;

	public String execute() {
		try {
			// TODO:判断Session

			// 执行请求
			return exe();
		} catch (Exception e) {
			return SUCCESS;
		}
	}

	public String exe() {

		if (getCommand() == null)
			setCommand("");

		if (getCommand().equals("dele"))
			delete();
		else if (getCommand().equals("add")) {
			return addnew();

		} else
			setPi(getBaseService().findAll(" from Users order by id desc",
					str2int(getPageNo()), str2int(getPageCount())));
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public void delete() {
		String queryString = " from Users where 1=1 ";
		queryString += "and id in (" + getIds() + "0 )";

		List list = getBaseService().findAll(queryString);
		for (Object obj : list)
			getBaseService().delete(obj);
		setResult(true);

	}

	public String addnew() {
		try {
			Users user;

			if (getId().length() > 0) {
				user = (Users) getBaseService().findByID("Users",
						Long.valueOf(str2int(getId())));
			} else
				user = new Users();

			user.setUserid(Long.valueOf(str2int(getUserid())));
			user.setUsername(getUsername());
			if (!getUserpass().equals(user.getUserpass()))	
			user.setUserpass(MD5Sec.MD5(getUserpass()));
			user.setMemo(getMemo());
			if (getId().length() > 0)
				getBaseService().merge(user);
			else
				getBaseService().save(user);
			return null;
		} catch (Exception e) {
			setRtntxt("保存失败！");
			// 返回一个错误信息
			return SUCCESS;
		}
	}

	// Getters and Setters 不必关心
	public PageInfo getPi() {
		return pi;
	}

	public void setPi(PageInfo pi) {
		this.pi = pi;
	}

	public String getPageNo() {
		return pageNo;
	}

	public void setPageNo(String pageNo) {
		this.pageNo = pageNo;
	}

	public String getPageCount() {
		return pageCount;
	}

	public void setPageCount(String pageCount) {
		this.pageCount = pageCount;
	}

	public String getCondi() {
		return condi;
	}

	public void setCondi(String condi) {
		this.condi = condi;
	}

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserpass() {
		return userpass;
	}

	public void setUserpass(String userpass) {
		this.userpass = userpass;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getRtntxt() {
		return rtntxt;
	}

	public void setRtntxt(String rtntxt) {
		this.rtntxt = rtntxt;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
